﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using SGP;
using System.Xml;
using System.Xml.Linq;
using System.IO;

namespace AnimationEditor
{
    public partial class Form1 : Form
    {
        DateTime fCurrent;
        DateTime fLast;
        //float fDelta;
        bool isLooping;
        bool isAnimating;
        int id;
        string filePath;
        string Pathline;
        bool isAnchor;
        bool isSourceRect;
        bool isCollisionRect;
        bool isAttackRect;
        bool isDrawing;

        bool isOnPanel;
        bool isPlaying;
        bool OutLooping;

        //  Point ScrollOffsets;
        Point StartPoint;
        Point EndPoint;
        Point StartPoint1;
        Point EndPoint1;
        Point StartPoint2;
        Point EndPoint2;
        Point StartPoint3;

        int ImageWidth;
        int ImageHeight;

        public int mousePosX;
        public int mousePosY;
        public int rectPosW;
        public int rectPosH;

        public Frames frames = new Frames();
        public Animation animation = new Animation();

        List<Frames> framesList = new List<Frames>();
        List<Animation> animationList = new List<Animation>();

        int DrawX = 0;
        int DrawY = 0;

        Point AnchorAnimation;
        Point AnchorOffset;
        //  Point AnchorOffsetAnimation;

        ManagedDirect3D d3dManager;
        ManagedTextureManager textManager;

        float changeintime;
        int framecounts;

        public Form1()
        {
            InitializeComponent();

            d3dManager = ManagedDirect3D.Instance;
            textManager = ManagedTextureManager.Instance;

            d3dManager.InitManagedDirect3D(panel2, false);
            d3dManager.InitManagedDirect3D(panel1, false);
            textManager.InitManagedTextureManager(d3dManager.Device, d3dManager.Sprite);
            framecounts = 0;
            changeintime = 0;
            OutLooping = false;
            id = -1;
            isLooping = true;
            isOnPanel = false;
            isDrawing = false;
        }

        public bool Looping()
        {
            return isLooping;
        }

        public void Updates()
        {
            fLast = fCurrent;
            fCurrent = DateTime.Now;
            if (isPlaying == true)
            {

                if ((0.001f * (fLast.Millisecond - fCurrent.Millisecond)) < 0)
                {
                    changeintime += -(0.001f * (fLast.Millisecond - fCurrent.Millisecond));
                }
                else
                    changeintime += 0.01f * (fLast.Millisecond - fCurrent.Millisecond);
            }
        }

        public void Render()
        {
            d3dManager.ChangeDisplayParam(panel1, false);

            d3dManager.DeviceBegin();
            d3dManager.SpriteBegin();

            d3dManager.Clear(Color.White);
            if (id != -1)
            {
                textManager.Draw(id, 0, 0, 1.0f, 1.0f, Rectangle.FromLTRB(DrawX, DrawY, DrawX + 1000, DrawY + 1000), 0, 0, 0, Color.White);
            }

            d3dManager.SpriteEnd();

            if (isOnPanel == true)
            {
                if (isSourceRect == true && isDrawing == true)
                {

                    d3dManager.DrawLine(StartPoint.X, StartPoint.Y, EndPoint.X, StartPoint.Y, Color.Red);
                    d3dManager.DrawLine(StartPoint.X, StartPoint.Y, StartPoint.X, EndPoint.Y, Color.Red);
                    d3dManager.DrawLine(EndPoint.X, StartPoint.Y, EndPoint.X, EndPoint.Y, Color.Red);
                    d3dManager.DrawLine(StartPoint.X, EndPoint.Y, EndPoint.X, EndPoint.Y, Color.Red);

                }

                if (isCollisionRect == true && isDrawing == true)
                {
                    d3dManager.DrawLine(StartPoint1.X, StartPoint1.Y, EndPoint1.X, StartPoint1.Y, Color.Green);
                    d3dManager.DrawLine(StartPoint1.X, StartPoint1.Y, StartPoint1.X, EndPoint1.Y, Color.Green);
                    d3dManager.DrawLine(EndPoint1.X, StartPoint1.Y, EndPoint1.X, EndPoint1.Y, Color.Green);
                    d3dManager.DrawLine(StartPoint1.X, EndPoint1.Y, EndPoint1.X, EndPoint1.Y, Color.Green);

                }

                if (isAttackRect == true && isDrawing == true)
                {
                    ManagedDirect3D.Instance.DrawLine(StartPoint2.X, StartPoint2.Y, EndPoint2.X, StartPoint2.Y, Color.Blue);
                    ManagedDirect3D.Instance.DrawLine(StartPoint2.X, StartPoint2.Y, StartPoint2.X, EndPoint2.Y, Color.Blue);
                    ManagedDirect3D.Instance.DrawLine(EndPoint2.X, StartPoint2.Y, EndPoint2.X, EndPoint2.Y, Color.Blue);
                    ManagedDirect3D.Instance.DrawLine(StartPoint2.X, EndPoint2.Y, EndPoint2.X, EndPoint2.Y, Color.Blue);

                }
                if (isAnchor == true && isDrawing == true)
                {


                    ManagedDirect3D.Instance.DrawLine(StartPoint3.X - 2, StartPoint3.Y - 2, StartPoint3.X + 2, StartPoint3.Y + 2, Color.Orange);
                    ManagedDirect3D.Instance.DrawLine(StartPoint3.X + 2, StartPoint3.Y + 2, StartPoint3.X - 2, StartPoint3.Y - 2, Color.Orange);
                }

            }

            ManagedDirect3D.Instance.DeviceEnd();

            ManagedDirect3D.Instance.Present();


            if (isPlaying == true)
            {
                if (FrameList.Items.Count > 0)
                {
                    d3dManager.ChangeDisplayParam(panel2, true);

                    AnchorAnimation.X = 10;
                    AnchorAnimation.Y = 10;

                    AnchorOffset.X = framesList[FrameList.SelectedIndex].SourceRect.X - framesList[FrameList.SelectedIndex].anchorPoint.X;
                    AnchorOffset.Y = framesList[FrameList.SelectedIndex].SourceRect.Y - framesList[FrameList.SelectedIndex].anchorPoint.Y;

                    d3dManager.DeviceBegin();
                    d3dManager.SpriteBegin();


                    d3dManager.Clear(Color.White);


                    textManager.Draw(id, AnchorAnimation.X - AnchorOffset.X, AnchorAnimation.Y - AnchorOffset.Y, 1.0f, 1.0f, framesList[FrameList.SelectedIndex].SourceRect, 0, 0, 0, 0);


                    d3dManager.SpriteEnd();
                    d3dManager.DeviceEnd();

                    //move forward
                    //needs six atm to run
                    if (framesList[FrameList.SelectedIndex].Durations < changeintime)
                    {
                        if (FrameList.SelectedIndex < animationList[AnimationList.SelectedIndex].FrameCounts - 1 && isAnimating == true)
                        {

                            FrameList.SelectedIndex++;

                            changeintime = 0;
                        }

                    }
                    d3dManager.Present();

                    //loop


                    if (FrameList.SelectedIndex == animationList[AnimationList.SelectedIndex].FrameCounts - 1 && isAnimating == true)
                    {
                        FrameList.SelectedIndex = 0;

                    }

                }

            }


        }

        private void NUDX_ValueChanged(object sender, EventArgs e)
        {
            framesList[FrameList.SelectedIndex].anchorPoint.X = (int)NUDX.Value;
        }

        private void NUDY_ValueChanged(object sender, EventArgs e)
        {
            framesList[FrameList.SelectedIndex].anchorPoint.Y = (int)NUDY.Value;
        }

        private void NUDFrameDuration_ValueChanged(object sender, EventArgs e)
        {
            framesList[FrameList.SelectedIndex].Durations = (float)NUDFrameDuration.Value;
            NUDFrameDuration.Value = (decimal)framesList[FrameList.SelectedIndex].Durations;
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                animationList[AnimationList.SelectedIndex].IfLooping = true;
                // OutLooping = true;
            }
            else
            {
                //OutLooping = false;
                animationList[AnimationList.SelectedIndex].IfLooping = false;
            }

        }

        private void NUDSourceLeft_ValueChanged(object sender, EventArgs e)
        {
            StartPoint.X = (int)NUDSourceLeft.Value;
            framesList[FrameList.SelectedIndex].SourceRect.X = (int)NUDSourceLeft.Value;
        }

        private void NUDSourceTop_ValueChanged(object sender, EventArgs e)
        {
            StartPoint.Y = (int)NUDSourceTop.Value;
            framesList[FrameList.SelectedIndex].SourceRect.Y = (int)NUDSourceTop.Value;
        }

        private void NUDSourceRight_ValueChanged(object sender, EventArgs e)
        {
            int rightValue;
            rightValue = EndPoint.X;
            rightValue = (int)NUDSourceRight.Value;
            // EndPoint.X = rightValue;
            framesList[FrameList.SelectedIndex].SourceRect.Width = (int)NUDSourceRight.Value;
        }

        private void NUDSourceBottom_ValueChanged(object sender, EventArgs e)
        {
            int botValue;
            botValue = StartPoint.Y + EndPoint.Y;
            botValue = (int)NUDSourceBottom.Value;
            botValue = EndPoint.Y;
            //     EndPoint.Y = botValue;

            framesList[FrameList.SelectedIndex].SourceRect.Height = (int)NUDSourceBottom.Value;
        }

        private void NUDCollisionLeft_ValueChanged(object sender, EventArgs e)
        {
            StartPoint1.X = (int)NUDCollisionLeft.Value;
            framesList[FrameList.SelectedIndex].CollisionRect.X = (int)NUDCollisionLeft.Value;
        }

        private void NUDCollisionTop_ValueChanged(object sender, EventArgs e)
        {
            StartPoint1.Y = (int)NUDCollisionTop.Value;
            framesList[FrameList.SelectedIndex].CollisionRect.Y = (int)NUDCollisionTop.Value;
        }

        private void NUDCollisionRight_ValueChanged(object sender, EventArgs e)
        {
            int rightValue2;
            rightValue2 = EndPoint1.X - StartPoint1.X;
            rightValue2 = (int)NUDCollisionRight.Value;
            //  EndPoint1.X= rightValue2;
            framesList[FrameList.SelectedIndex].CollisionRect.Width = (int)NUDCollisionRight.Value;
        }

        private void NUDCollisionBottom_ValueChanged(object sender, EventArgs e)
        {
            int botValue2;
            botValue2 = EndPoint1.Y + StartPoint1.Y;
            botValue2 = (int)NUDCollisionBottom.Value;

            botValue2 = EndPoint2.Y;
            // EndPoint1.Y = botValue2;
            framesList[FrameList.SelectedIndex].CollisionRect.Height = (int)NUDCollisionBottom.Value;
        }

        private void NUDAttackLeft_ValueChanged(object sender, EventArgs e)
        {
            StartPoint2.X = (int)NUDAttackLeft.Value;
            framesList[FrameList.SelectedIndex].AttackRect.X = (int)NUDAttackLeft.Value;
        }

        private void NUDAttackTop_ValueChanged(object sender, EventArgs e)
        {
            StartPoint2.Y = (int)NUDAttackTop.Value;
            framesList[FrameList.SelectedIndex].AttackRect.Y = (int)NUDAttackTop.Value;
        }

        private void NUDAttackRight_ValueChanged(object sender, EventArgs e)
        {
            int rightValue1;
            rightValue1 = EndPoint2.X - StartPoint2.X;
            rightValue1 = (int)NUDAttackRight.Value;
            //  EndPoint2.X = rightValue1;
            framesList[FrameList.SelectedIndex].AttackRect.Width = (int)NUDAttackRight.Value;
        }

        private void NUDAttackBottom_ValueChanged(object sender, EventArgs e)
        {
            int botValue1;
            botValue1 = EndPoint2.Y + StartPoint2.Y;
            botValue1 = (int)NUDAttackBottom.Value;

            botValue1 = EndPoint2.Y;
            // EndPoint2.Y=botValue1;
            framesList[FrameList.SelectedIndex].AttackRect.Height = (int)NUDAttackBottom.Value;
        }

        private void NewAnimationButton_Click(object sender, EventArgs e)
        {
            List<Frames> tempFrames = new List<Frames>(framesList);

            Animation addAnimation = new Animation();

            addAnimation.Frames = tempFrames;
            addAnimation.FrameCounts = 0;
            addAnimation.IfLooping = false;
            addAnimation.FilePath1 = " ";
            animationList.Add(addAnimation);

            AnimationList.Items.Add("Animation" + (animationList.Count - 1));
            AnimationList.SelectedIndex = animationList.Count - 1;
            framecounts = 0;


            //FrameList.Items.Clear();
            // framesList.Clear();
        }

        private void RemoveAnimationButton_Click(object sender, EventArgs e)
        {
            int temp = animationList[AnimationList.SelectedIndex].FrameCounts;
            int newloc = 0;
            for (int i = temp; i > 0; i--)
            {
                framesList.RemoveAt(i - 1);
                animationList[AnimationList.SelectedIndex].Frames.RemoveAt(i - 1);
                animationList[AnimationList.SelectedIndex].FrameCounts -= 1;
            }
            if (AnimationList.Items.Count > 0)
            {
                if (AnimationList.SelectedIndex != 0)
                    newloc = AnimationList.SelectedIndex - 1;
                else
                    newloc = 0;
            }
            else
                newloc = -1;
            animationList.RemoveAt(AnimationList.SelectedIndex);
            AnimationList.Items.RemoveAt(AnimationList.SelectedIndex);
            if (AnimationList.Items.Count == 0)
                AnimationList.SelectedIndex = -1;
            else
                AnimationList.SelectedIndex = newloc;
            AnimationList.Refresh();

            if (newloc != -1)
            {
                if (AnimationList.Items.Count != 0)
                {
                    if (animationList[AnimationList.SelectedIndex].FrameCounts > 0)
                        FrameList.SelectedIndex = 0;
                    else
                        FrameList.SelectedIndex = -1;
                }
            }
            FrameList.Refresh();

        }

        private void EditAnimationButton_Click(object sender, EventArgs e)
        {
            if (AnimationList.Items.Count != 0)
            {
                AnimationList.SelectedIndex = 0;
                if (animationList[0].FrameCounts != 0)
                {
                    SaveFileDialog sdlg = new SaveFileDialog();

                    if (DialogResult.OK == sdlg.ShowDialog())
                    {

                        //fix this up....
                        filePath = sdlg.FileName;
                        XElement root = new XElement("AnimationSet");

                        for (int i = 0; i < animationList.Count(); i++)
                        {
                            if (animationList[i].FrameCounts != 0)
                            {

                                XElement animations = new XElement("Animation");
                                XAttribute AnimationName = new XAttribute("Name", animationList[i].FilePath1.ToString());

                                XAttribute FrameCounts = new XAttribute("FrameCount", animationList[i].FrameCounts);
                                XAttribute ImageIds = new XAttribute("ImageId", Pathline.ToString());
                                XAttribute ISLooping;
                                if (OutLooping == true)
                                    ISLooping = new XAttribute("Looping", "True");
                                else
                                    ISLooping = new XAttribute("Looping", "False");

                                animations.Add(AnimationName);
                                animations.Add(FrameCounts);
                                animations.Add(ImageIds);
                                animations.Add(ISLooping);

                                for (int x = 0; x < animationList[i].FrameCounts; x++)
                                {
                                    XElement frames = new XElement("Frame");

                                    XElement sourceRect = new XElement("SourceRECT");

                                    Frames frameObj = new Frames();


                                    XAttribute sourceLeft = new XAttribute("RenderLeft", animationList[i].Frames[x].SourceRect.Left);
                                    XAttribute sourceTop = new XAttribute("RenderTop", animationList[i].Frames[x].SourceRect.Top);
                                    XAttribute sourceRight = new XAttribute("RenderRight", animationList[i].Frames[x].SourceRect.Right);
                                    XAttribute sourceBottom = new XAttribute("RenderBottom", animationList[i].Frames[x].SourceRect.Bottom);

                                    sourceRect.Add(sourceLeft);
                                    sourceRect.Add(sourceTop);
                                    sourceRect.Add(sourceRight);
                                    sourceRect.Add(sourceBottom);




                                    XElement collisionRect = new XElement("CollisionRECT");
                                    //subtract value from height width

                                    int L, T, R, B;
                                    L = animationList[i].Frames[x].CollisionRect.Left;
                                    T = animationList[i].Frames[x].CollisionRect.Top;
                                    R = animationList[i].Frames[x].CollisionRect.Right;
                                    B = animationList[i].Frames[x].CollisionRect.Bottom;

                                    if (L == 0 && T == 0 && R == 0 && B == 0)
                                    {
                                        XAttribute collisionLeft = new XAttribute("CollisionLeft", L);
                                        XAttribute collisionTop = new XAttribute("CollisionTop", T);
                                        XAttribute collisionRight = new XAttribute("CollisionRight", R);
                                        XAttribute collisionBottom = new XAttribute("CollisionBottom", B);
                                        collisionRect.Add(collisionLeft);
                                        collisionRect.Add(collisionTop);
                                        collisionRect.Add(collisionRight);
                                        collisionRect.Add(collisionBottom);
                                    }
                                    else
                                    {
                                        XAttribute collisionLeft = new XAttribute("CollisionLeft", ImageWidth - animationList[i].Frames[x].CollisionRect.Left);
                                        XAttribute collisionTop = new XAttribute("CollisionTop", ImageHeight - animationList[i].Frames[x].CollisionRect.Top);
                                        XAttribute collisionRight = new XAttribute("CollisionRight", ImageWidth - animationList[i].Frames[x].CollisionRect.Right);
                                        XAttribute collisionBottom = new XAttribute("CollisionBottom", ImageHeight - animationList[i].Frames[x].CollisionRect.Bottom);
                                        collisionRect.Add(collisionLeft);
                                        collisionRect.Add(collisionTop);
                                        collisionRect.Add(collisionRight);
                                        collisionRect.Add(collisionBottom);
                                    }

                                    XElement attRect = new XElement("AttackRECT");

                                    L = 0; T = 0; R = 0; B = 0;
                                    L = animationList[i].Frames[x].AttackRect.Left;
                                    T = animationList[i].Frames[x].AttackRect.Top;
                                    R = animationList[i].Frames[x].AttackRect.Right;
                                    B = animationList[i].Frames[x].AttackRect.Bottom;

                                    if (L == 0 && T == 0 && R == 0 && B == 0)
                                    {

                                        XAttribute attLeft = new XAttribute("AttackLeft", L);
                                        XAttribute attTop = new XAttribute("AttackTop",T);
                                        XAttribute attRight = new XAttribute("AttackRight", R);
                                        XAttribute attBottom = new XAttribute("AttackBottom", B);
                                        attRect.Add(attLeft);
                                        attRect.Add(attTop);
                                        attRect.Add(attRight);
                                        attRect.Add(attBottom);
                                    }
                                    else
                                    {
                                        XAttribute attLeft = new XAttribute("AttackLeft", ImageWidth - animationList[i].Frames[x].AttackRect.Left);
                                        XAttribute attTop = new XAttribute("AttackTop", ImageHeight - animationList[i].Frames[x].AttackRect.Top);
                                        XAttribute attRight = new XAttribute("AttackRight", ImageWidth - animationList[i].Frames[x].AttackRect.Right);
                                        XAttribute attBottom = new XAttribute("AttackBottom", ImageHeight - animationList[i].Frames[x].AttackRect.Bottom);
                                        attRect.Add(attLeft);
                                        attRect.Add(attTop);
                                        attRect.Add(attRight);
                                        attRect.Add(attBottom);
                                    }

                                    L = 0; T = 0; R = 0; B = 0;
                                    XElement datas = new XElement("Data");

                                    XAttribute AnchorX = new XAttribute("AnchorX", (animationList[i].Frames[x].anchorPoint.X - (ImageWidth - animationList[i].Frames[x].SourceRect.Left)));
                                    XAttribute AnchorY = new XAttribute("AnchorY", (animationList[i].Frames[x].anchorPoint.Y - (ImageHeight - animationList[i].Frames[x].SourceRect.Top)));
                                    XAttribute Duration = new XAttribute("Duration", animationList[i].Frames[x].Durations);
                                    datas.Add(AnchorX);
                                    datas.Add(AnchorY);
                                    datas.Add(Duration);
                                    XAttribute trigger = new XAttribute("EventTrigger", "NO");
                                    datas.Add(trigger);
                                    //  trigger.Value = "NO";

                                    //  trigger.Add(trigger.Value);


                                    frames.Add(sourceRect);
                                    frames.Add(collisionRect);
                                    frames.Add(attRect);
                                    frames.Add(datas);


                                    animations.Add(frames);
                                }
                                root.Add(animations);
                            }

                        }
                        root.Save(filePath);
                    }
                }
            }
        }

        private void NewFrameButton_Click(object sender, EventArgs e)
        {
            if (AnimationList.SelectedIndex != -1)
            {
                Frames addFrame = new Frames();
                framecounts += 1;
                addFrame.SourceRect = Rectangle.Empty;
                addFrame.anchorPoint = Point.Empty;
                addFrame.AttackRect = Rectangle.Empty;
                addFrame.CollisionRect = Rectangle.Empty;
                addFrame.trigger = "No";
                animationList[AnimationList.SelectedIndex].Frames.Add(addFrame);
                animationList[AnimationList.SelectedIndex].FrameCounts++;
                framesList.Add(addFrame);
                FrameList.Items.Add("Frame" + (framesList.Count - 1));
                FrameList.SelectedIndex = framesList.Count - 1;

                isAnchor = false;
                isAttackRect = false;
                isCollisionRect = false;
            }
        }

        private void RemoveFrameButton_Click(object sender, EventArgs e)
        {
            if (FrameList.SelectedIndex != -1)
            {
                framesList.RemoveAt(FrameList.SelectedIndex);
                animationList[AnimationList.SelectedIndex].Frames.RemoveAt(FrameList.SelectedIndex);
                FrameList.Items.RemoveAt(FrameList.SelectedIndex);
                animationList[AnimationList.SelectedIndex].FrameCounts -= 1;
                FrameList.SelectedIndex = animationList[AnimationList.SelectedIndex].FrameCounts - 1;
            }
            //  AnimationList.Refresh();
            //  FrameList.Refresh();

        }
        private void EditFrameButton_Click(object sender, EventArgs e)
        {

            framesList[FrameList.SelectedIndex].anchorPoint.X = (int)NUDX.Value;
            framesList[FrameList.SelectedIndex].anchorPoint.Y = (int)NUDY.Value;
            NUDX.Value = framesList[FrameList.SelectedIndex].anchorPoint.X;
            NUDY.Value = framesList[FrameList.SelectedIndex].anchorPoint.Y;
            framesList[FrameList.SelectedIndex].Durations = (float)NUDFrameDuration.Value;
            NUDFrameDuration.Value = (decimal)framesList[FrameList.SelectedIndex].Durations;


        }

        private void PlayButton_Click(object sender, EventArgs e)
        {
            if (id != -1)
            {
                //  currentframe = FrameList.SelectedIndex;
                isPlaying = true;
                isAnimating = true;
            }
        }

        private void StopButton_Click(object sender, EventArgs e)
        {
            if (id != -1)
            {
                isPlaying = false;
                isAnimating = false;
            }
        }

        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void imageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            isAnchor = false;
            isSourceRect = false;
            isCollisionRect = false;
            isAttackRect = false;
            OpenFileDialog fdlg = new OpenFileDialog();

            if (DialogResult.OK == fdlg.ShowDialog())
            {
                filePath = fdlg.FileName;
                id = ManagedTextureManager.Instance.LoadTexture(filePath, 0);
                ImageWidth = ManagedTextureManager.Instance.GetTextureWidth(id);
                ImageHeight = ManagedTextureManager.Instance.GetTextureHeight(id);
                //                  NUDX.Maximum = ImageWidth;
                //  NUDY.Maximum = ImageHeight;
            }
        }

        private void fromXmlToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {

            isLooping = false;

        }

        private void LoadButton_Click(object sender, EventArgs e)
        {
            isAnchor = false;
            isSourceRect = false;
            isCollisionRect = false;
            isAttackRect = false;
            OpenFileDialog fdlg = new OpenFileDialog();

            if (DialogResult.OK == fdlg.ShowDialog())
            {

                filePath = fdlg.FileName;
                Pathline = fdlg.SafeFileName;
                id = ManagedTextureManager.Instance.LoadTexture(filePath, 0);

                ImageWidth = ManagedTextureManager.Instance.GetTextureWidth(id);
                ImageHeight = ManagedTextureManager.Instance.GetTextureHeight(id);
                NUDX.Maximum = ImageWidth;
                NUDY.Maximum = ImageHeight;
            }
        }

        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            if (animationList.Capacity != 0 && framesList.Capacity != 0)
            {
                if (isAnchor == true)
                {
                    StartPoint3.X = e.Location.X;
                    StartPoint3.Y = e.Location.Y;

                }
                else if (isSourceRect == true)
                {

                    StartPoint.X = e.X;
                    StartPoint.Y = e.Y;


                    frames.SourceRect.X = e.X;
                    frames.SourceRect.Y = e.Y;

                    NUDSourceLeft.Value = frames.SourceRect.X;
                    NUDSourceTop.Value = frames.SourceRect.Y;

                    isOnPanel = true;
                    isSourceRect = true;


                    if (NUDSourceLeft.Value < 0)
                    {
                        NUDSourceLeft.Value *= -1;
                    }
                    if (NUDSourceTop.Value < 0)
                    {
                        NUDSourceTop.Value *= -1;
                    }


                }
                else if (isAttackRect == true)
                {
                    StartPoint2.X = e.X;
                    StartPoint2.Y = e.Y;


                    frames.AttackRect.X = e.X;
                    frames.AttackRect.Y = e.Y;

                    NUDAttackLeft.Value = frames.AttackRect.X;
                    NUDAttackTop.Value = frames.AttackRect.Y;

                    isOnPanel = true;
                    isAttackRect = true;

                    if (NUDAttackLeft.Value < 0)
                    {
                        NUDAttackLeft.Value *= -1;
                    }
                    if (NUDAttackTop.Value < 0)
                    {
                        NUDAttackTop.Value *= -1;
                    }



                }
                else if (isCollisionRect == true)
                {


                    StartPoint1.X = e.X;
                    StartPoint1.Y = e.Y;

                    frames.CollisionRect.X = e.X;
                    frames.CollisionRect.Y = e.Y;

                    NUDCollisionLeft.Value = frames.CollisionRect.X;
                    NUDCollisionTop.Value = frames.CollisionRect.Y;

                    isOnPanel = true;

                    if (NUDCollisionLeft.Value < 0)
                    {
                        NUDCollisionLeft.Value *= -1;
                    }
                    if (NUDCollisionTop.Value < 0)
                    {
                        NUDCollisionTop.Value *= -1;
                    }


                }
            }
        }

        private void panel1_MouseUp(object sender, MouseEventArgs e)
        {

            if (animationList.Capacity != 0 && framesList.Capacity != 0)
            {

                if (isAnchor == true)
                {
                    frames.anchorPoint.X = StartPoint3.X;
                    frames.anchorPoint.Y = StartPoint3.Y;

                    NUDX.Value = frames.anchorPoint.X;
                    NUDY.Value = frames.anchorPoint.Y;
                    framesList[FrameList.SelectedIndex].anchorPoint.X = (int)NUDX.Value;
                    framesList[FrameList.SelectedIndex].anchorPoint.Y = (int)NUDY.Value;

                }
                else if (isSourceRect == true)
                {
                    EndPoint.X = e.X;
                    EndPoint.Y = e.Y;

                    frames.SourceRect.Width = e.X - frames.SourceRect.X;
                    frames.SourceRect.Height = e.Y - frames.SourceRect.Y;

                    NUDSourceRight.Value = frames.SourceRect.Width;
                    NUDSourceBottom.Value = frames.SourceRect.Height;
                    isDrawing = true;
                    isSourceRect = true;

                    if (NUDSourceRight.Value < 0)
                    {
                        NUDSourceRight.Value *= -1;
                        NUDSourceLeft.Value -= NUDSourceRight.Value;
                    }
                    if (NUDSourceBottom.Value < 0)
                    {
                        NUDSourceBottom.Value *= -1;
                        NUDSourceTop.Value -= NUDSourceBottom.Value;
                    }

                    framesList[FrameList.SelectedIndex].SourceRect.X = (int)NUDSourceLeft.Value;
                    framesList[FrameList.SelectedIndex].SourceRect.Y = (int)NUDSourceTop.Value;
                    framesList[FrameList.SelectedIndex].SourceRect.Width = (int)NUDSourceRight.Value;
                    framesList[FrameList.SelectedIndex].SourceRect.Height = (int)NUDSourceBottom.Value;

                }
                else if (isAttackRect == true)
                {
                    EndPoint2.X = e.X;
                    EndPoint2.Y = e.Y;

                    frames.AttackRect.Width = e.X - frames.AttackRect.X;
                    frames.AttackRect.Height = e.Y - frames.AttackRect.Y;

                    NUDAttackRight.Value = frames.AttackRect.Width;
                    NUDAttackBottom.Value = frames.AttackRect.Height;

                    isDrawing = true;
                    isAttackRect = true;

                    if (NUDAttackRight.Value < 0)
                    {
                        NUDAttackRight.Value *= -1;
                        NUDAttackLeft.Value -= NUDAttackRight.Value;
                    }
                    if (NUDAttackBottom.Value < 0)
                    {
                        NUDAttackBottom.Value *= -1;
                        NUDAttackTop.Value -= NUDAttackBottom.Value;
                    }
                    framesList[FrameList.SelectedIndex].AttackRect.X = (int)NUDAttackLeft.Value;
                    framesList[FrameList.SelectedIndex].AttackRect.Y = (int)NUDAttackTop.Value;
                    framesList[FrameList.SelectedIndex].AttackRect.Width = (int)NUDAttackRight.Value;
                    framesList[FrameList.SelectedIndex].AttackRect.Height = (int)NUDAttackBottom.Value;

                }
                else if (isCollisionRect == true)
                {

                    EndPoint1.X = e.X;
                    EndPoint1.Y = e.Y;

                    frames.CollisionRect.Width = e.X - frames.CollisionRect.X;
                    frames.CollisionRect.Height = e.Y - frames.CollisionRect.Y;

                    NUDCollisionRight.Value = frames.CollisionRect.Width;
                    NUDCollisionBottom.Value = frames.CollisionRect.Height;

                    isDrawing = true;
                    isCollisionRect = true;


                    if (NUDCollisionRight.Value < 0)
                    {
                        NUDCollisionRight.Value *= -1;
                        NUDCollisionLeft.Value -= NUDCollisionRight.Value;
                    }
                    if (NUDCollisionBottom.Value < 0)
                    {
                        NUDCollisionBottom.Value *= -1;
                        NUDCollisionTop.Value -= NUDCollisionBottom.Value;
                    }

                    framesList[FrameList.SelectedIndex].CollisionRect.X = (int)NUDCollisionLeft.Value;
                    framesList[FrameList.SelectedIndex].CollisionRect.Y = (int)NUDCollisionTop.Value;
                    framesList[FrameList.SelectedIndex].CollisionRect.Width = (int)NUDCollisionRight.Value;
                    framesList[FrameList.SelectedIndex].CollisionRect.Height = (int)NUDCollisionBottom.Value;
                }
            }
        }

        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            mousePosX = e.X;
            mousePosY = e.Y;


            if (isSourceRect == true && e.Button == MouseButtons.Left)
            {
                EndPoint.X = e.X;
                EndPoint.Y = e.Y;
                isDrawing = true;
            }
            else if (isAttackRect == true && e.Button == MouseButtons.Left)
            {
                EndPoint2.X = e.X;
                EndPoint2.Y = e.Y;
                isDrawing = true;
            }
            else if (isCollisionRect == true && e.Button == MouseButtons.Left)
            {
                EndPoint1.X = e.X;
                EndPoint1.Y = e.Y;
                isDrawing = true;
            }
            else if (isAnchor == true && e.Button == MouseButtons.Left)
            {
                StartPoint3.X = e.X;
                StartPoint3.Y = e.Y;
                isDrawing = true;
            }

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            isLooping = false;
        }

        private void hScrollBar1_Scroll(object sender, ScrollEventArgs e)
        {
            int hPos1 = e.OldValue;
            int hPos2 = e.NewValue;

            if (hPos2 > hPos1)
            {
                DrawX += hPos1;
                hPos2 = hPos1 - DrawX;
            }
            else if (hPos2 < hPos1)
            {
                DrawX -= hPos1;
                hPos2 = hPos1 + DrawX;
            }

        }

        private void vScrollBar1_Scroll(object sender, ScrollEventArgs e)
        {
            int hPos1 = e.OldValue;
            int hPos2 = e.NewValue;

            if (hPos2 > hPos1)
            {
                DrawY += hPos1;
                hPos2 = hPos1;
            }
            else if (hPos2 < hPos1)
            {
                DrawY -= hPos1;
                hPos2 = hPos1;
            }

        }

        private void AnchorPointButton_Click(object sender, EventArgs e)
        {
            isAnchor = true;
            isSourceRect = false;
            isCollisionRect = false;
            isAttackRect = false;
            if (FrameList.Items.Count > 0)
            {
                StartPoint3.X = framesList[FrameList.SelectedIndex].anchorPoint.X;
                StartPoint3.Y = framesList[FrameList.SelectedIndex].anchorPoint.Y;
            }
        }

        private void SourceRectButton_Click(object sender, EventArgs e)
        {
            isAnchor = false;
            isSourceRect = true;
            isCollisionRect = false;
            isAttackRect = false;
            if (FrameList.Items.Count > 0)
            {
                StartPoint.X = framesList[FrameList.SelectedIndex].SourceRect.Left;
                StartPoint.Y = framesList[FrameList.SelectedIndex].SourceRect.Top;
                EndPoint.X = framesList[FrameList.SelectedIndex].SourceRect.Right;
                EndPoint.Y = framesList[FrameList.SelectedIndex].SourceRect.Bottom;
            }
        }

        private void CollidingRectButton_Click(object sender, EventArgs e)
        {

            isAnchor = false;
            isSourceRect = false;
            isCollisionRect = true;
            isAttackRect = false;
            if (FrameList.Items.Count > 0)
            {
                StartPoint1.X = framesList[FrameList.SelectedIndex].CollisionRect.Left;
                StartPoint1.Y = framesList[FrameList.SelectedIndex].CollisionRect.Top;
                EndPoint1.X = framesList[FrameList.SelectedIndex].CollisionRect.Right;
                EndPoint1.Y = framesList[FrameList.SelectedIndex].CollisionRect.Bottom;
            }
        }

        private void AttackRectButton_Click(object sender, EventArgs e)
        {
            isAnchor = false;
            isSourceRect = false;
            isCollisionRect = false;
            isAttackRect = true;
            if (FrameList.Items.Count > 0)
            {

                StartPoint2.X = framesList[FrameList.SelectedIndex].AttackRect.Left;
                StartPoint2.Y = framesList[FrameList.SelectedIndex].AttackRect.Top;
                EndPoint2.X = framesList[FrameList.SelectedIndex].AttackRect.Right;
                EndPoint2.Y = framesList[FrameList.SelectedIndex].AttackRect.Bottom;
            }
        }

        private void FrameList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (FrameList.SelectedIndex != -1)
            {
                NUDX.Value = framesList[FrameList.SelectedIndex].anchorPoint.X;
                NUDY.Value = framesList[FrameList.SelectedIndex].anchorPoint.Y;
                NUDAttackLeft.Value = framesList[FrameList.SelectedIndex].AttackRect.X;
                NUDAttackTop.Value = framesList[FrameList.SelectedIndex].AttackRect.Y;
                NUDAttackRight.Value = framesList[FrameList.SelectedIndex].AttackRect.Width;
                NUDAttackBottom.Value = framesList[FrameList.SelectedIndex].AttackRect.Height;
                NUDCollisionLeft.Value = framesList[FrameList.SelectedIndex].CollisionRect.X;
                NUDCollisionTop.Value = framesList[FrameList.SelectedIndex].CollisionRect.Y;
                NUDCollisionRight.Value = framesList[FrameList.SelectedIndex].CollisionRect.Width;
                NUDCollisionBottom.Value = framesList[FrameList.SelectedIndex].CollisionRect.Height;
                NUDSourceLeft.Value = framesList[FrameList.SelectedIndex].SourceRect.X;
                NUDSourceTop.Value = framesList[FrameList.SelectedIndex].SourceRect.Y;
                NUDSourceRight.Value = framesList[FrameList.SelectedIndex].SourceRect.Width;
                NUDSourceBottom.Value = framesList[FrameList.SelectedIndex].SourceRect.Height;
                textBox1.Text = framesList[FrameList.SelectedIndex].trigger.ToString();
                NUDFrameDuration.Value = (decimal)framesList[FrameList.SelectedIndex].Durations;
                StartPoint3.X = framesList[FrameList.SelectedIndex].anchorPoint.X;
                StartPoint3.Y = framesList[FrameList.SelectedIndex].anchorPoint.Y;
                StartPoint.X = framesList[FrameList.SelectedIndex].SourceRect.Left;
                StartPoint.Y = framesList[FrameList.SelectedIndex].SourceRect.Top;
                EndPoint.X = framesList[FrameList.SelectedIndex].SourceRect.Right;
                EndPoint.Y = framesList[FrameList.SelectedIndex].SourceRect.Bottom;
                StartPoint2.X = framesList[FrameList.SelectedIndex].AttackRect.Left;
                StartPoint2.Y = framesList[FrameList.SelectedIndex].AttackRect.Top;
                EndPoint2.X = framesList[FrameList.SelectedIndex].AttackRect.Right;
                EndPoint2.Y = framesList[FrameList.SelectedIndex].AttackRect.Bottom;
                StartPoint1.X = framesList[FrameList.SelectedIndex].CollisionRect.Left;
                StartPoint1.Y = framesList[FrameList.SelectedIndex].CollisionRect.Top;
                EndPoint1.X = framesList[FrameList.SelectedIndex].CollisionRect.Right;
                EndPoint1.Y = framesList[FrameList.SelectedIndex].CollisionRect.Bottom;

            }

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            framesList[FrameList.SelectedIndex].trigger = textBox1.Text.ToString();
        }

        private void AnimationList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (AnimationList.SelectedIndex != -1)
            {
                framesList.Clear();
                FrameList.Items.Clear();

                if (animationList[AnimationList.SelectedIndex].FrameCounts != 0)
                {
                    int counts = animationList[AnimationList.SelectedIndex].FrameCounts;
                    for (int i = 0; i < counts; i++)
                    {
                        framesList.Add(animationList[AnimationList.SelectedIndex].Frames[i]);
                        FrameList.Items.Add("Frame" + (framesList.Count - 1));

                    }
                    FrameList.SelectedIndex = 0;
                }
                if (animationList[AnimationList.SelectedIndex].IfLooping == true)
                {
                    checkBox1.Checked = true;
                }
                else
                    checkBox1.Checked = false;

                textBox2.Text = animationList[AnimationList.SelectedIndex].FilePath1.ToString();
            }
        }
        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            animationList[AnimationList.SelectedIndex].FilePath1 = textBox2.Text.ToString();
        }

        private void xMLToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int i = 0;
            int f = 0;

            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "All Files|*.*|Xml Files|*.xml";
            dlg.FilterIndex = 2;

            if (DialogResult.OK == dlg.ShowDialog())
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(dlg.FileName);

                foreach (XmlElement Animation in doc.SelectNodes("AnimationSet"))
                {

                    foreach (XmlElement Frame in doc.SelectNodes("AnimationSet/Animation"))
                    {

                    }

                }

            }

        }

    }
    //MLB
}
